package com.chixing.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.chixing.entity.Coupon;
import com.chixing.mapper.CouponMapper;
import com.chixing.service.ICouponService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chixing.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class CouponServiceImpl  implements ICouponService {
    @Autowired
    private CouponMapper couponMapper;



    //查询所有优惠卷
    @Override
    public Result getAll() {
        // 查询所有未过期的优惠券
        QueryWrapper<Coupon> wrapper = new QueryWrapper<>();
        wrapper.gt("coupon_end_time", LocalDateTime.now());
        List<Coupon> couponList = couponMapper.selectList(wrapper);

        if (couponList != null && couponList.size() > 0) {
            return Result.getSuccess(couponList);
        }
        return Result.getFail("暂无优惠券数据");
    }


    //根据优惠卷的id查询优惠卷
    @Override
    public Result getCouponById(Integer couponId) {
        // 查询单个优惠券，根据 couponId
        Coupon coupon = couponMapper.selectById(couponId);
        if (coupon != null) {
            return Result.getSuccess(coupon);
        }
        return Result.getFail("没有找到该优惠券");
    }






    // 获取所有优惠券分类
    @Override
    public Result getCouponTypes() {
        // 查询优惠券分类，假设 coupon 表中有 coupon_type 字段
        QueryWrapper<Coupon> wrapper = new QueryWrapper<>();
        wrapper.select("DISTINCT coupon_type"); // 查询不重复的优惠券类型

        List<Coupon> couponTypes = couponMapper.selectList(wrapper);

        if (couponTypes != null && couponTypes.size() > 0) {
            return Result.getSuccess(couponTypes);
        }
        return Result.getFail("暂无优惠券类型数据");
    }



    //根据类型名查询
    @Override
    public Result getCouponsByType(String type) {
        QueryWrapper<Coupon> wrapper = new QueryWrapper<>();
        wrapper.eq("coupon_type", type);
        List<Coupon> list = couponMapper.selectList(wrapper);

        if (list != null && !list.isEmpty()) {
            return Result.getSuccess(list);
        }
        return Result.getFail("没有该类型的优惠券");
    }






}
